package com.yc.baseapi;

import cn.hutool.core.util.URLUtil;

import java.math.BigDecimal;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class HandleString {
	
	public void testSplit() {
		String a = "a,b,c,";
		
		String[] b = a.split(",");
		System.out.println(b.length);
	}
	
	/**
	 * 字符串包含
	 */
	public void testContains() {
		String x1 = "abcdefg";
		String x2 = "cdef";
		String x3 = "abcdefg";
		System.out.println(x1.contains(x2));
		System.out.println(x1.contains(x3));
		System.out.println(x2.contains(x1));
	}
	
	public void testEquals() {
		String s1 = "abc";
		String s2 = "abc";
		Object o2 = "abc";
		
		long a1 = 2L;
		Object o3 = 2L;
		
		System.out.println(s1.equals(s1));
		System.out.println(Long.valueOf(a1).equals(o3));
		
	}
	
	public void testSubstring() {
		String a = "uetplatform_2020-05-27_0.log";
		String b = "uetplatform_2020-05-27_12.log";
		
		System.out.println(a.substring(23, a.length()-4));
		System.out.println(b.substring(23, b.length()-4));
	}
	
	public void m3() {
		System.out.println(new DecimalFormat("#.##%").format(70*1.0/100));
	}
	
	public Integer getQrcodeType_v1(String qrcode) {
		if (qrcode.length() != 20)
			throw new RuntimeException("二维码格式错误");
		
		switch(qrcode.charAt(17)) {
		case '0':
			return 0;
		case '1':
			return 1;
		case '2':
			return 2;
		default:
			return null;
		}
		
	}

	public void string2Byte() {
		String s = "6";
		Byte b = Byte.valueOf(s);
		System.out.println(b);

		Short st = Short.valueOf("6");

		Integer i = Integer.valueOf("6");

		System.out.println(st.equals(i));
		System.out.println(st.equals(b));

		Byte b2 = Byte.parseByte("6");
		System.out.println(b.equals(b2));

		byte[] bytes = "6".getBytes();
		System.out.println(bytes[0]);


	}


	/**
	 * 页面拼接字符串经验：
	 * 无论表单是否有值，都在后面拼接一个逗号
	 * 后台正常split后会有一个问题：后面多的连续逗号会被忽略，所以要遍历一个不会出现空置的
	 * 然后尝试取可能有空置的，如果下标已经越界，那么这个值就是空
	 */
	public void m4() {
		String s = "aa,bb,cc";
		String s1 = "aa,bb,cc,";
		String s2 = ",aa,bb,cc,";
		String s3 = ",,aa,bb,cc,";
		String s4 = ",aa,bb,cc,,";
		String s5 = ",aa,bb,cc,,dd,";
		String s6 = ",aa,bb,cc,,,dd,ee,";
		String s7 = ",aa,bb,cc,,,,,";

		String[] a = s.split(",");
		String[] a1 = s1.split(",");
		String[] a2 = s2.split(",");
		String[] a3 = s3.split(",");
		String[] a4 = s4.split(",");
		String[] a5 = s5.split(",");
		String[] a6 = s6.split(",");
		String[] a7 = s7.split(",");
		System.out.println("-----------");
	}


	public void m5() {
		String str = "a*b";
		char x = '*';
		System.out.println(x==str.charAt(1));
	}


	public void m6() {
		String a = "身份证照片%@%身份证照片2%@%";
		String[] aa = a.split("%@%");
		System.out.println(aa.length);
 	}

	/**
	 * 正则表达式 [] 表示匹配一个字符，这种方式 特殊字符不用转义
	 */
	public void m7() {
		String a = "身份证照片.身份证|照片2";
//		String[] aa = a.split("[.]");
		String[] aa = a.split("[|]");
		System.out.println(aa.length);

		String b = "aaa||bbb";
		String[] bb = b.split("\\|\\|");
		System.out.println(bb.length);

		String c = "aaa:bbb";
		String[] cc = c.split(":");
		System.out.println(cc.length);
	}

	/**
	 * 有一个参数，一个返回值
	 * @param param
	 * @return
	 */
	public String m8(String param) {
		System.out.println("---param:" + param);
		try {
			Thread.sleep(10000);
			System.out.println("---over:");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return param;
	}

	/**
	 * 有一个参数，但没有返回值
	 * Consumer<String>
	 * @param param
	 */
	public void m9(String param) {
		System.out.println("---param:" + param);
	}

	/**
	 * 没有参数，但有一个返回值
	 * Supplier<String>
	 * @return
	 */
	public String m10() {
		System.out.println("---param:");
		return "hello10";
	}

	public void m11() {
		String a = "c607bf1e-6875-4b8b-b42e-44408a35690f";
		String b = a.replaceAll("-", "");
		System.out.println(b);
	}

	private void m12() {

	}

	public void getCityByLogId() {
		String startAddr = "浙江省杭州市西湖区蒋村街道紫霞街91号杭州互联网创新创业园";
		int indexS = 0;
		if (startAddr.indexOf("省") != -1) {
			indexS = startAddr.indexOf("省") + 1;
			if (indexS >= startAddr.length()) {
				System.out.println("---1");
			}
		}

		int indexE = 0;
		if (startAddr.indexOf("市") != -1) {
			indexE = startAddr.indexOf("市");
		} else {
			System.out.println("---2");
		}

		System.out.println(startAddr.substring(indexS, indexE));
	}

	public void m13() {
		String partnerNo = "12_"+System.currentTimeMillis();
		int id = 0;
		try {
			id = Integer.parseInt(partnerNo.split("_")[0]);
		} catch (Exception e) {
			throw new RuntimeException("partnerNo不存在");
		}
		System.out.println(id);
	}

	public void m14() {
		String s = "\"wODDqAnF4LLJVm/WBIr0Mg==\"";
		String st = s.replace("\"","");
		System.out.println(st);
	}

	public void m15() {
		String s = "wODDqAnF4LLJVm/WBIr0Mg==";
		System.out.println(URLUtil.encodeAll(s));
	}

	//替换\r\n -->  \n
	public void m16() {
		String str = "1）本券仅限更换机滤机油及相应工时费用  \r\n2）每张券仅限1车使用1次\r\n3）机油超过4L或更换更高级别，需另补差价，具体差价可咨询商户  ";
		String str2 = "1）本券仅限更换机滤机油及相应工时费用  \n2）每张券仅限1车使用1次\r\n3）机油超过4L或更换更高级别，需另补差价，具体差价可咨询商户  ";
		String str3 = "1）本券仅限更换机滤机油及相应工时费用  \n2）每张券仅限1车使用1次 \n3）机油超过4L或更换更高级别，需另补差价，具体差价可咨询商户  ";
		String str4 = "1）本券仅限更换机滤机油及相应工时费用  2）每张券仅限1车使用1次 3）机油超过4L或更换更高级别，需另补差价，具体差价可咨询商户  ";

//		str = str.replaceAll("\r", "");
//		System.out.println(str);
//
//		str2 = str2.replaceAll("\r", "");
//		System.out.println(str2);
//
//		str3 = str3.replaceAll("\r", "");
//		System.out.println(str3);
//
//		str4 = str4.replaceAll("\r", "");
//		System.out.println(str4);

		String tips = defaluStr(null).replaceAll("\r", "");
		System.out.println(tips);
	}

	private String defaluStr(String str) {
		return (str==null)?"":str;
	}

	//Stringbuffer
	public void m17() {
		StringBuffer sb1 = new StringBuffer("abcdef");
		sb1.delete(1, 3);
		System.out.println(sb1);//adef
		StringBuffer sb2 = new StringBuffer("abcdef");
		sb2.deleteCharAt(1);
		System.out.println(sb2);//acdef
	}

	public void m18() {
		String routingKey = "delay.xiaoju";
		String msgType = routingKey.substring(6);
		System.out.println(msgType);
	}

	public void m19() {
		String a = "2022-03,2022-01,2023-01";
//		String a = "2022-03,2022-01";
//		String a = "2022-03";
		String b = "'" + a.replaceAll(",", "','") + "'";
		System.out.println(b);
	}

	public void m20() {
		Set<Integer> peoples = new HashSet<>();
		peoples.add(2);
		peoples.add(4);
		System.out.println("peoples:" + peoples);

		List<String> permitUsers = new ArrayList<>();
		permitUsers.add("aa");
		permitUsers.add("bb");
		System.out.println("permitUsers:" + permitUsers);
	}

	public void m21() {
		String a = "123";
		StringBuffer sb = new StringBuffer("abc");
		sb.reverse();
		System.out.println("sb2:" + sb.toString());

	}


	public static void main(String[] args) {
		HandleString hs = new HandleString();
//		hs.testSplit();
//		hs.testContains();
//		hs.testEquals();
//		hs.testSubstring();
//		hs.m3();
//		System.out.println(hs.getQrcodeType_v1("01234567891234567290"));
//		hs.string2Byte();
//		hs.m4();
//		hs.m5();
//		hs.m6();
//		hs.m7();
//		hs.m11();
//		hs.getCityByLogId();
//		hs.m13();
//		hs.m14();
//		hs.m15();
//		hs.m16();
//		hs.m17();
//		hs.m18();
//		hs.m19();
//		hs.m20();
		hs.m21();
	}


}
